﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Xml.Serialization;

namespace Common
{
    public class DBSetting
    {
        /// <summary>
        /// 获取配置字符串
        /// </summary>
        public static string ConText
        {
            get { return System.Configuration.ConfigurationManager.AppSettings["ConnectString"]; }
        }

        /// <summary>
        /// 反序列化
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public static T DeSerializerByString<T>(byte[] bytes)
        {
            try
            {
                BinaryFormatter Formatter = new BinaryFormatter();
                MemoryStream ms = new MemoryStream(bytes);
                //Formatter.Binder = new UBinder();
                ms.Position = 0;
                object Obj = Formatter.Deserialize(ms);
                ms.Close();
                return (T)Obj;
            }
            catch
            {
                return default(T);
            }
        }

        /// <summary>
        /// 序列化对象成字符串
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <returns></returns>
        public static byte[] SerializerByObject<T>(T t)
        {
            MemoryStream ms = new MemoryStream();
            try
            {
                string str = string.Empty;
                //DataContractJsonSerializer ds = new DataContractJsonSerializer(typeof(T));
                //ds.WriteObject(ms, t);
                //strJson = Encoding.UTF8.GetString(ms.ToArray());
                BinaryFormatter Formatter = new BinaryFormatter();
                Formatter.Serialize(ms, t);
                byte[] bytes = ms.ToArray();
                ms.Close();

                return bytes;
            }
            catch
            {
                ms.Close();
                return null;
            }
        }
        public static T getObject<T>(Stream stream)
        {
            T obj;
            try
            {
                StreamReader sr = new StreamReader(stream, System.Text.Encoding.UTF8);
                XmlSerializer _xs = new XmlSerializer(typeof(T));
                string value = sr.ReadToEnd();
                Byte[] bytes = Encoding.UTF8.GetBytes(value);
                Stream sm = new MemoryStream(bytes);
                StreamReader reader = new StreamReader(sm, Encoding.UTF8);
                obj = (T)_xs.Deserialize(reader);
            }
            catch (Exception er)
            {
                throw er;
            }
            return obj;
        }
        /// <summary>
        /// 序列化对象成XML
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <returns></returns>
        public static string SerializerXmlByObject<T>(T t)
        {
            MemoryStream ms = new MemoryStream();
            try
            {
                string str = string.Empty;
                XmlSerializer xs = new XmlSerializer(typeof(T));

                xs.Serialize(ms, t);
                str = Encoding.UTF8.GetString(ms.ToArray());
                ms.Close();
                return str;
            }
            catch
            {
                ms.Close();
                return string.Empty;
            }
        }
        public static string SerializerXmlByObject(object t)
        {
            MemoryStream ms = new MemoryStream();
            try
            {
                Type ts = t.GetType();
                string str = string.Empty;
                XmlSerializer xs = new XmlSerializer(ts);

                xs.Serialize(ms, t);
                str = Encoding.UTF8.GetString(ms.ToArray());
                ms.Close();
                return str;
            }
            catch
            {
                ms.Close();
                return string.Empty;
            }
        }
    }
}
